//
//  PKSocialShareView.swift
//  Panku
//
//  Created by mac on 2017/2/13.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit
import SnapKit
import YYKit

private let PKSocialShareViewCellID = "PKSocialShareViewCellID"
class PKSocialShareView: PKBaseView ,UICollectionViewDelegate,UICollectionViewDataSource{
    
    static let sharedInstance = PKSocialShareView()
    
    private override init() {
        super.init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - properties
    //UI
    //maskView
    lazy var coverView = PKBaseView()
    lazy var containerView = PKBaseView()
    
    //dismissButton
    lazy var dismissButton = PKBaseButton.createButton()
    //collectionView
    lazy var collectionView:PKBaseCollectionView = PKBaseCollectionView { (layout) in
        layout.itemSize = CGSize(width: 60*D_FACTOR_X, height: 60*D_FACTOR_X)
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 20
        layout.minimumInteritemSpacing = 15
        layout.sectionInset = UIEdgeInsets(top: 23, left: 40, bottom: 0, right: 40)
    }
    
    //MARK: - UI
    override func setupView() -> Void{
        
        //self
        self.backgroundColor = UIColor.clear
        
        self.addSubview(coverView)
        self.addSubview(containerView)
        self.containerView.addSubview(collectionView)
        self.containerView.addSubview(dismissButton)
        
        coverView.backgroundColor = UIColor(hexString: "00000088")
        coverView.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        
        dismissButton.backgroundColor = UIColor.black
        dismissButton.addBlock(for: .touchUpInside) { (_) in
            //TODO:dismiss
            PKSocialShareView.dismiss()
        }
        
        containerView.backgroundColor = UIColor.clear
        containerView.frame = CGRect(x: 0, y: APP_HEIGHT, width: APP_WIDTH, height: 205)
        
        dismissButton.setImage(R.image.socialShare_dismiss(), for: .normal)
        dismissButton.snp.makeConstraints { (make) in
            make.bottom.equalTo(containerView.snp.bottom)
            make.left.equalTo(containerView.snp.left)
            make.right.equalTo(containerView.snp.right)
            make.height.equalTo(44)
        }
        
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(PKSocialShareViewCell.self, forCellWithReuseIdentifier: PKSocialShareViewCellID)
        collectionView.snp.makeConstraints { (make) in
            make.left.equalTo(containerView.snp.left)
            make.right.equalTo(containerView.snp.right)
            make.top.equalTo(containerView.snp.top)
            make.bottom.equalTo(containerView.snp.bottom)
        }
    }
    
    //MARK: - showIn and dismiss
    //showIn
    class func showIn(view:UIView = KEY_WINDOW!) -> Void{
        let socialShareView = PKSocialShareView.sharedInstance
        socialShareView.frame = CGRect(x: 0, y: 0, width: APP_WIDTH, height: APP_HEIGHT)
        
        view.addSubview(socialShareView)
        socialShareView.containerView.frame = CGRect(x: 0, y: APP_HEIGHT, width: APP_WIDTH, height: 205)
        socialShareView.coverView.alpha = 0
        UIView.animate(withDuration: 0.25) { 
            socialShareView.containerView.frame = CGRect(x: 0, y: APP_HEIGHT-205, width: APP_WIDTH, height: 205)
            socialShareView.coverView.alpha = 1
        }
    }
    
    //dismiss
    class func dismiss() -> Void{
        let socialShareView = PKSocialShareView.sharedInstance
        
        socialShareView.containerView.frame = CGRect(x: 0, y: APP_HEIGHT-205, width: APP_WIDTH, height: 205)
        socialShareView.coverView.alpha = 1
        UIView.animate(withDuration: 0.25, animations: { 
            socialShareView.containerView.frame = CGRect(x: 0, y: APP_HEIGHT, width: APP_WIDTH, height: 205)
            socialShareView.coverView.alpha = 0
        }) { (_) in
            socialShareView.removeFromSuperview()
        }
    }

}

//collectionView dataSource delegate
extension PKSocialShareView{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 6
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PKSocialShareViewCellID, for: indexPath)
        
        var image:UIImage?
        switch indexPath.item {
        case 0:
            image = R.image.socialShare_timeline()
        case 1:
            image = R.image.socialShare_wechat()
        case 2:
            image = R.image.socialShare_qq()
        case 3:
            image = R.image.socialShare_zone()
        case 4:
            image = R.image.socialShare_weibo()
        case 5:
            image = R.image.socialShare_link()
        default:
            image = UIImage()
        }
        
        (cell as! PKSocialShareViewCell).iconView.image = image
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath)
        
        PKSocialShareView.dismiss()
    }
}

class PKSocialShareViewCell: PKBaseCollectionViewCell {
    
    //MARK: - properties
    //UI
    lazy var iconView = UIImageView()
    
    override func setupCell() -> Void{
        contentView.addSubview(iconView)
        
        iconView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        
    }
}
